package servlet.denglu;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import service.denglu.impl.Dl_BizDaoServiceImpl;
import dao.impl.denglu.Dl_BizDaoImpl;
import entity.denglu.Dl_ZhuCe;

/**
 * Servlet implementation class Dl_Servlet
 */
public class Dl_Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * 
	 * @see HttpServlet#HttpServlet()
	 */
	public Dl_Servlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String duser = request.getParameter("duser");
		String dpwd = request.getParameter("dpwd");
		PrintWriter out = response.getWriter();
		HttpSession session=request.getSession();

		Dl_BizDaoImpl dlbd = new Dl_BizDaoImpl();
		Dl_BizDaoServiceImpl dlbsi = new Dl_BizDaoServiceImpl();
		dlbsi.setDl_dBizDao(dlbd);

		Dl_ZhuCe dzc = new Dl_ZhuCe();
		dzc.setUser(duser);
		dzc.setPwd(dpwd);
		
		int did=0;
		int id=0;
		String str="";
		boolean flag = dlbsi.getAll(dzc);
		List<Dl_ZhuCe> listdl=dlbsi.getXinXi(dzc);
		for (Dl_ZhuCe dlz : listdl) {
			id=dlz.getId();
			did=dlz.getQid();
			str=dlz.getPermission();
		}
		
		if (flag) {
			session.setAttribute("duser", duser);
			session.setAttribute("quanx",str );
			session.setAttribute("did", did);
			session.setAttribute("dl_id", id);
			out.print("<script>");
			out.print("location.href='sl_store/systemIndexPage.jsp';");
			out.print("</script>");
			out.flush();
			out.close();
		} else {
			out.print("<script>");
			out.print("alert('用户名或密码错误');");
			out.print("location.href='sl_store/index.jsp';");
			out.print("</script>");
			out.flush();
			out.close();
		}
	}

}
